package willevaluate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import view.MyConfirmDialog;
import willevaluate.adapter.QuestionnaireProvAdapter;
import willevaluate.bean.QuestionnaireProvBean;
import willevaluate.model.QuestionnaireProvModel;

/**
 * 问卷调查的地区和科目多选列表
 */

@ContentView(R.layout.activity_questionnaire_prov)
public class QuestionnaireProv_Subject extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.common_detail)
    private TextView common_detail;
    @ViewInject(R.id.questionnaireProv_lv)
    private ListView questionnaireProv_lv;
    @ViewInject(R.id.questionnaireProv_confirm)
    private Button questionnaireProv_confirm;
    private QuestionnaireProvAdapter questionnaireProvAdapter;
    private ArrayList<QuestionnaireProvModel> arrayList;
    private ArrayList<QuestionnaireProvModel> subjectList;
    private ArrayList<String> provsList;
    private myHandler myHandler;
    private final int GET_DATA = 0;
    private String subject;//文科理科
    private String adv_subject;//优势学科
    private MyConfirmDialog myConfirmDialog;

    private static class myHandler extends Handler {
        WeakReference<QuestionnaireProv_Subject> questionnaireProvWeakReference;

        public myHandler(QuestionnaireProv_Subject activity) {
            questionnaireProvWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            QuestionnaireProv_Subject questionnaireProv = questionnaireProvWeakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    QuestionnaireProvBean questionnaireProvBean = gson.fromJson((String) msg.obj, QuestionnaireProvBean.class);
                    if (questionnaireProvBean != null) {
                        QuestionnaireProvBean.DataBean data = questionnaireProvBean.getData();
                        if (data != null) {
                            List<String> hot = data.getHot();
                            List<String> normal = data.getNormal();
                            if (hot != null) {
                                //写死部分
                                QuestionnaireProvModel questionnaireProvModel0 = new QuestionnaireProvModel();
                                questionnaireProvModel0.setPro_name("想去什么省份上大学");
                                questionnaireProvModel0.setType(QuestionnaireProvAdapter.TYPE_GROUP);
                                questionnaireProv.arrayList.add(questionnaireProvModel0);
                                QuestionnaireProvModel questionnaireProvModel00 = new QuestionnaireProvModel();
                                questionnaireProvModel00.setPro_name("本省(广东)");
                                questionnaireProvModel00.setChecked(true);
                                questionnaireProvModel00.setType(QuestionnaireProvAdapter.TYPE_ITEM);
                                questionnaireProv.arrayList.add(questionnaireProvModel00);

                                QuestionnaireProvModel questionnaireProvModel = new QuestionnaireProvModel();
                                questionnaireProvModel.setPro_name("热门省份");
                                questionnaireProvModel.setType(QuestionnaireProvAdapter.TYPE_GROUP);
                                questionnaireProv.arrayList.add(questionnaireProvModel);

                                for (int i = 0; i < hot.size(); i++) {
                                    QuestionnaireProvModel questionnaireProvModel1 = new QuestionnaireProvModel();
                                    questionnaireProvModel1.setPro_name(hot.get(i));
                                    questionnaireProvModel1.setType(QuestionnaireProvAdapter.TYPE_ITEM);
                                    questionnaireProv.arrayList.add(questionnaireProvModel1);
                                }
                            }
                            if (normal != null) {
                                QuestionnaireProvModel questionnaireProvModel2 = new QuestionnaireProvModel();
                                questionnaireProvModel2.setPro_name("其他省份");
                                questionnaireProvModel2.setType(QuestionnaireProvAdapter.TYPE_GROUP);
                                questionnaireProv.arrayList.add(questionnaireProvModel2);
                                for (int i = 0; i < normal.size(); i++) {
                                    QuestionnaireProvModel questionnaireProvModel3 = new QuestionnaireProvModel();
                                    questionnaireProvModel3.setPro_name(normal.get(i));
                                    questionnaireProvModel3.setType(QuestionnaireProvAdapter.TYPE_ITEM);
                                    questionnaireProv.arrayList.add(questionnaireProvModel3);
                                }
                            }
                            for (int i = 0; i < questionnaireProv.arrayList.size(); i++) {
                                if (questionnaireProv.provsList.contains(questionnaireProv.arrayList.get(i).getPro_name())) {
                                    questionnaireProv.arrayList.get(i).setChecked(true);
                                }
                            }
                            questionnaireProv.questionnaireProvAdapter.notifyDataSetChanged();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        subject = getIntent().getStringExtra("subject");
        adv_subject = getIntent().getStringExtra("adv_subject");
        if (TextUtils.isEmpty(subject)) {
            initData();
            initView();
            getData();
        } else {
            initSubjectData();
            initView();
        }

    }

    private void initData() {
        String prov = getIntent().getStringExtra("prov");
        provsList = new ArrayList<>();
        if (prov != null) {
            String[] provs = prov.split("/");
            if (provs != null) {
                for (int i = 0; i < provs.length; i++) {
                    provsList.add(provs[i]);
                }
            }
        }
    }

    private void initSubjectData() {
        subjectList = new ArrayList<>();
        QuestionnaireProvModel questionnaireProvModel = new QuestionnaireProvModel();
        questionnaireProvModel.setPro_name("语文");
        questionnaireProvModel.setType(QuestionnaireProvAdapter.TYPE_ITEM);
        subjectList.add(questionnaireProvModel);
        QuestionnaireProvModel questionnaireProvModel1 = new QuestionnaireProvModel();
        questionnaireProvModel1.setPro_name("数学");
        questionnaireProvModel1.setType(QuestionnaireProvAdapter.TYPE_ITEM);
        subjectList.add(questionnaireProvModel1);
        QuestionnaireProvModel questionnaireProvModel2 = new QuestionnaireProvModel();
        questionnaireProvModel2.setPro_name("英语");
        questionnaireProvModel2.setType(QuestionnaireProvAdapter.TYPE_ITEM);
        subjectList.add(questionnaireProvModel2);
        if ("文科".equals(subject)) {
            QuestionnaireProvModel questionnaireProvModel4 = new QuestionnaireProvModel();
            questionnaireProvModel4.setPro_name("政治");
            questionnaireProvModel4.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel4);
            QuestionnaireProvModel questionnaireProvModel5 = new QuestionnaireProvModel();
            questionnaireProvModel5.setPro_name("历史");
            questionnaireProvModel5.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel5);
            QuestionnaireProvModel questionnaireProvModel6 = new QuestionnaireProvModel();
            questionnaireProvModel6.setPro_name("地理");
            questionnaireProvModel6.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel6);
        } else {
            QuestionnaireProvModel questionnaireProvModel4 = new QuestionnaireProvModel();
            questionnaireProvModel4.setPro_name("物理");
            questionnaireProvModel4.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel4);
            QuestionnaireProvModel questionnaireProvModel5 = new QuestionnaireProvModel();
            questionnaireProvModel5.setPro_name("化学");
            questionnaireProvModel5.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel5);
            QuestionnaireProvModel questionnaireProvModel6 = new QuestionnaireProvModel();
            questionnaireProvModel6.setPro_name("生物");
            questionnaireProvModel6.setType(QuestionnaireProvAdapter.TYPE_ITEM);
            subjectList.add(questionnaireProvModel6);
        }
        ArrayList<String> subs = new ArrayList<>();
        if (adv_subject != null) {
            String[] subjects = adv_subject.split("/");
            if (subjects != null) {
                for (int i = 0; i < subjects.length; i++) {
                    subs.add(subjects[i]);
                }
            }
        }
        for (int i = 0; i < subjectList.size(); i++) {
            if (subs.contains(subjectList.get(i).getPro_name())) {
                subjectList.get(i).setChecked(true);
            }
        }
    }

    private void initView() {
        myHandler = new myHandler(this);
        common_back.setOnClickListener(this);
        common_detail.setVisibility(View.VISIBLE);
        common_detail.setText("清空");
        questionnaireProv_confirm.setOnClickListener(this);
        common_detail.setOnClickListener(this);
        if (TextUtils.isEmpty(subject)) {
            //省份选择
            common_title.setText("学校省份");
            arrayList = new ArrayList<>();
            questionnaireProvAdapter = new QuestionnaireProvAdapter(arrayList, this);
            questionnaireProv_lv.setAdapter(questionnaireProvAdapter);
            questionnaireProv_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    arrayList.get(position).setChecked(!arrayList.get(position).isChecked());
                    questionnaireProvAdapter.notifyDataSetChanged();
                }
            });
        } else {
            //科目选择
            common_title.setText("擅长科目");
            questionnaireProvAdapter = new QuestionnaireProvAdapter(subjectList, this);
            questionnaireProv_lv.setAdapter(questionnaireProvAdapter);
            questionnaireProv_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    subjectList.get(position).setChecked(!subjectList.get(position).isChecked());
                    questionnaireProvAdapter.notifyDataSetChanged();
                }
            });
        }

    }

    @Override
    public void onBackPressed() {
        myConfirmDialog = new MyConfirmDialog(this, "是否保存", "是", "否");
        myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                MyConfirm();
            }
        });
        myConfirmDialog.setCancelListener(new MyConfirmDialog.CancelListener() {
            @Override
            public void cancel() {
                QuestionnaireProv_Subject.this.finish();
            }
        });
        myConfirmDialog.show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                myConfirmDialog = new MyConfirmDialog(this, "是否保存", "是", "否");
                myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
                    @Override
                    public void confirm() {
                        MyConfirm();
                    }
                });
                myConfirmDialog.setCancelListener(new MyConfirmDialog.CancelListener() {
                    @Override
                    public void cancel() {
                        QuestionnaireProv_Subject.this.finish();
                    }
                });
                myConfirmDialog.show();
                break;
            case R.id.common_detail:
                cleanList();
                break;
            case R.id.questionnaireProv_confirm:
                MyConfirm();
                break;
            default:
                break;
        }
    }

    private void cleanList() {
        if (TextUtils.isEmpty(subject)) {
            for (QuestionnaireProvModel questionnaireProvModel :
                    arrayList) {
                questionnaireProvModel.setChecked(false);
            }
        } else {
            for (QuestionnaireProvModel questionnaireProvModel :
                    subjectList) {
                questionnaireProvModel.setChecked(false);
            }
        }
        questionnaireProvAdapter.notifyDataSetChanged();
    }

    private void MyConfirm() {
        if (TextUtils.isEmpty(subject)) {
            //选择省份
            String provs = "";
            StringBuffer stringBuffer = new StringBuffer(provs);
            for (int i = 0; i < arrayList.size(); i++) {
                if (arrayList.get(i).isChecked()) {
                    stringBuffer.append("/" + arrayList.get(i).getPro_name());
                }
            }
            provs = stringBuffer.toString();
            String substring;
            if (provs.length() > 1) {
                substring = provs.substring(1, provs.length());
            } else {
                substring = provs;
            }
            Intent i = new Intent();
            i.putExtra("prov", substring);
            setResult(1, i);
        } else {
            //选择优势学科
            String subs = "";
            StringBuffer stringBuffer = new StringBuffer(subs);
            for (int i = 0; i < subjectList.size(); i++) {
                if (subjectList.get(i).isChecked()) {
                    stringBuffer.append("/" + subjectList.get(i).getPro_name());
                }
            }
            subs = stringBuffer.toString();
            String substring;
            if (subs.length() > 1) {
                substring = subs.substring(1, subs.length());
            } else {
                substring = subs;
            }
            Intent i = new Intent();
            i.putExtra("subject", substring);
            setResult(1, i);
        }
        this.finish();
        myConfirmDialog = null;
    }

    private void getData() {
        new InterNetController(this, Constant.MUTABLESELECTPROV, myHandler, null, GET_DATA);
    }

}
